import { mgop } from '@aligov/jssdk-mgop';
const defaultSettings = require('../settings.js');
export default {
  // 封装浙里办get请求
  get(data){
    const token = sessionStorage.getItem('token') || '';
    return new Promise((resolve,reject) => {
      mgop({
        api: process.env.VUE_APP_ZLB_URL || defaultSettings.VUE_APP_ZLB_URL, // 必填
        host: process.env.NODE_ENV === 'production'?process.env.VUE_APP_URL:'/api',
        dataType: 'JSON',
        type: 'GET',
        header:{
          'X-Auth-Token': `Bearer ${ token }` || '',
          'X-Requested-With': 'XMLHttpRequest'
        },
        data,
        appKey: process.env.VUE_APP_ZLB_KEY || defaultSettings.VUE_APP_ZLB_KEY, // 必填
        onSuccess: data => {
          resolve(data.data);
        },
        onFail: err => {
          reject(err);
        }
      });
    });
  },

  // 封装浙里办post请求
  post(data){
    const token = sessionStorage.getItem('token') || '';
    return new Promise((resolve,reject) => {
      mgop({
        api: process.env.VUE_APP_ZLB_URL || defaultSettings.VUE_APP_ZLB_URL, // 必填
        host: process.env.NODE_ENV === 'production'?process.env.VUE_APP_URL:'/api',
        dataType: 'JSON',
        type: 'POST',
        header:{
          'X-Auth-Token':`Bearer ${ token }` || '',
          'X-Requested-With': 'XMLHttpRequest'
        },
        data,
        appKey: process.env.VUE_APP_ZLB_KEY || defaultSettings.VUE_APP_ZLB_KEY,
        onSuccess: data => {
          resolve(data.data);
        },
        onFail: err => {
          reject(err);
        }
      });
    });
  },

  // 封装浙里办delete请求
  delete(data){
    const token = sessionStorage.getItem('token') || '';
    return new Promise((resolve,reject) => {
      mgop({
        api: process.env.VUE_APP_ZLB_URL || defaultSettings.VUE_APP_ZLB_URL, // 必填
        host: process.env.NODE_ENV === 'production'?process.env.VUE_APP_URL:'/api',
        dataType: 'JSON',
        type: 'DELETE',
        header:{
          'X-Auth-Token':`Bearer ${ token }` || '',
          'X-Requested-With': 'XMLHttpRequest'
        },
        data,
        appKey: process.env.VUE_APP_ZLB_KEY || defaultSettings.VUE_APP_ZLB_KEY,
        onSuccess: data => {
          resolve(data.data);
        },
        onFail: err => {
          reject(err);
        }
      });
    });
  },

  // 封装浙里办put请求
  put(data){
    const token = sessionStorage.getItem('token') || '';
    return new Promise((resolve,reject) => {
      mgop({
        api: process.env.VUE_APP_ZLB_URL || defaultSettings.VUE_APP_ZLB_URL, // 必填
        host: process.env.NODE_ENV === 'production'?process.env.VUE_APP_URL:'/api',
        dataType: 'JSON',
        type: 'PUT',
        header:{
          'X-Auth-Token':`Bearer ${ token }` || '',
          'X-Requested-With': 'XMLHttpRequest'
        },
        data,
        appKey: process.env.VUE_APP_ZLB_KEY || defaultSettings.VUE_APP_ZLB_KEY,
        onSuccess: data => {
          resolve(data.data);
        },
        onFail: err => {
          reject(err);
        }
      });
    });
  },
};
